﻿/*! 
 *  MyMSDN locale module
 */
(function ($, window, undefined) {
    'use strict';

    var document = window.document, hasOwnProperty = Object.prototype.hasOwnProperty;

    var Cookies = function () {
        this.init();
    };
    Cookies.prototype = {
        init: function () {
            this._cc = {};

            if (document.cookie.length) {
                var cc = document.cookie.split(';');
                var i;
                for (i = 0; i < cc.length; i++) {
                    var cookie = cc[i].split('=');
                    this._cc[cookie[0]] = cookie[1];
                }
            }
        },
        get: function (key) {
            return this._cc[key];
        },
        set: function (key, value) {
            this._cc[key] = value;

            var cc = [];
            for (var k in this._cc) {
                if (hasOwnProperty.call(this._cc, k)) {
                    cc.push(k + '=' + this._cc[k]);
                }
            }
            document.cookie = cc.join(';');
        }
    };
    var __Cookies_instance;
    Cookies.instance = function () {
        if (!__Cookies_instance)
            __Cookies_instance = new Cookies();
        return __Cookies_instance;
    };

    var Locale = function () {
        this.init();
    };
    Locale.prototype = {
        init: function () {
            this._onchangelocale = null;
            this._lcc = ['en-US', 'zh-CN'];

            this._cc = Cookies.instance();
            this._lc = this._cc.get('lc') || navigator.language || navigator.userLanguage || this._lcc[0];
            if ($.inArray(this._lc, this._lcc) === -1) {
                this._lc = this._lcc[0];
            }

            this.onchangelocale(this.refresh, this);
        },
        get: function () {
            return this._lc;
        },
        set: function (value) {
            if ($.inArray(value, this._lcc) !== -1) {
                this._lc = value;
                this._cc.set('lc', value);

                if (this._onchangelocale) {
                    var e = this._onchangelocale;
                    e.handle.call(e.context || null, value);
                }
            }
        },
        onchangelocale: function (handle, context) {
            this._onchangelocale = { handle: handle, context: context };
        },
        refresh: function (lc, root) {
            var res = Locale[lc];
            $("[data-lc]", root || $(document)).each(function () {
                var e = $(this);
                var c = e.data('lc').split(',');
                for (var i = 0; i < c.length; i++) {
                    var cc = c[i].split(':');
                    var rs = res[cc[1]];
                    switch (cc[0]) {
                        case 'v':
                            e.text(rs);
                            break;
                        case 't':
                            e.attr("title", rs);
                            break;
                        case 'p':
                            e.attr("placeholder", rs);
                            break;
                        default:
                            break;
                    }
                }
            });
        }
    };
    var __Locale_instance;
    Locale.instance = function () {
        if (!__Locale_instance)
            __Locale_instance = new Locale();
        return __Locale_instance;
    };

    // expose
    window.Locale = Locale;

})(jQuery, window);
